<?php require "mysql.php"; ?>
<?php require "common_functions.php"; ?>
<?php

function find_all($from, $hash=null){
  //
  // SELECT $select FROM $from WHERE $hash["conditions"] LIMIT $hash["limit"] ORDER BY $hash["order"];
  //
  // $id_field = $hash["id"];
  // $select = $hash["select"];
  // $where = $hash["conditions"];
  // $order = $hash["order"];
  // $group = $hash["group"];
  // $limit = $hash["limit"];
  // $offset = $hash["offset"]; // DO NOT to use at this time.
  // Examples:
  //          mysql_query(find_all("news"));
  //          mysql_query(find_all("members", array("conditions" => "name = 'CFC'", "limit" => 10, "order" => "id DESC")));
	
	if(is_json($hash)) $hash = json_decode($hash, true);
	return parse_options($from, $hash);
}

function find_all_by_field($field, $value, $from, $hash=null){
  //
  // SELECT $select FROM $from WHERE $hash["conditions"] LIMIT $hash["limit"] ORDER BY $hash["order"];
  //
  // $id_field = $hash["id"];
  // $select = $hash["select"];
  // $where = $hash["conditions"];
  // $order = $hash["order"];
  // $group = $hash["group"];
  // $limit = $hash["limit"];
  // $offset = $hash["offset"]; // DO NOT to use at this time.
  // Examples:
  //          mysql_query(find_all_by_field("id", 10, "users"));
  //          mysql_query(find_all_by_field("name", "cfc", "admins"));
  //          mysql_query(find_all_by_field("sex", "female", "members", array("limit" => 10)))
	
	if(is_json($hash)) $hash = json_decode($hash, true);
	if(check_option($hash["conditions"]))
	  $hash["conditions"] = $hash["conditions"] . " AND ".$field." = '".$value."'";
	else
	  $hash["conditions"] = $field." = '".$value."'";
	return parse_options($from, $hash);
}

// 
// INSERT INTO `admins`(`account`, `password`, `name`) VALUES('cfc', 'cfc', 'Billy Hsu');
// INSERT INTO $table_name($fields) VALUES($values);
// Examples:
//   mysql_query(create("admin", array("account" => "cfc", "password" => "cfc", "name" => "Billy Hsu")));
// 

function create($table_name, $fields_and_values){
  $ary = handle_fields_and_values($fields_and_values);
  $ary = split("_S_", $ary);
  $sql.= "INSERT INTO ";
  $sql.= "`".$table_name."`";
  $sql.= $ary[0];
  $sql.= " VALUES".$ary[1];
  
  return $sql;
}

function update_field($from, $field, $value, $where=null){
  // 
  //
  // UPDATE $from SET $field = $value WHERE $where
  //
  // Examples:
  //   UPDATE `admins` a SET a.account = 'CFC';
  //   #=> mysql_query(update_field("`admins` a", "a.account", "CFC"));
  //   UPDATE `admins` SET account = 'CFC' WHERE id = 1;
  //   #=> mysql_query(update_field("admins", "account", "CFC", "id = 1"));
  // 
  // 
  
  if(check_option($where))
    $where = " WHERE ".$where;
  
  $sql = "UPDATE ";
  $sql.= $from;
  $sql.= " SET ";
  $sql.= $field;
  $sql.= "=";
  $sql.= "'".$value."'";
  $sql.= $where;

  return $sql;
}

function update_fields($from, $fields, $where=null){
  // 
  //
  // UPDATE $from SET $field = $value WHERE $where
  //
  // Examples:
  //   UPDATE `admins` SET account = 'CFC', password = 'OK' WHERE id = 1;
  //   #=> mysql_query(update_fields("admins", array('account' => 'cfc', 'password' => 'OK'), "id = 1"));
  // 
  // 
  
  if(is_array($fields))
	  $fields = handle_fields($fields);
  else
    return false;

  if(check_option($where))
    $where = " WHERE ".$where;
  
  $sql = "UPDATE ";
  $sql.= "`".$from."`";
  $sql.= " SET ";
  $sql.= $fields;
  $sql.= $where;

  return $sql;
}

function delete_by($from, $where=null){
  // 
  // DELETE FROM $from WHRER $where
  // 
  // Examples:
  //   DELETE FROM `admins` WHERE id = 1;
  //   mysql_query(delete_by('admins', 'id = 1'));
  //   DELETE FROM `admins`
  //   mysql_query(delete_by('admins'));

  if(check_option($where))
    $where = " WHERE ".$where;
  $sql.= "DELETE FROM ";
  $sql.= "`".$from."`";
  $sql.= $where;

  return $sql;
}
?>